package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.Adv;
import com.kgd.news.entity.Smallapp;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtils;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/10/24
 * @Time: 下午11:03
 */
public class AdvController extends BaseController {

    @Override
    public void list() {
        String sql = "from adv where type = 1";
        String categoryid = getPara("categoryid");
        String title = getPara("title");
        String type = "adv";
        if (!StringUtils.isEmpty(categoryid)) {
            if ("-1".equals(categoryid)) {
                sql += " and categoryid is null";
            } else {
                sql += " and categoryid =" + categoryid;
            }
            type += categoryid;
        }
        if (!StringUtils.isEmpty(title)) {
            sql += " and title like '%" + title + "%'";
            type += title;
        }
        sql += " order by id desc";
        Page<Adv> data = Adv.dao.paginate(getPageIndex(type), 50, "select *", sql);
        setAttr("data", data);
        render("adv_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        String categoryid = getPara("categoryid");
        Adv adv;
        if (StringUtils.isEmpty(categoryid)) {
             adv = Adv.dao.findById(id);
        } else {
            adv=  Adv.dao.findFirst("select * from adv where categoryid = ?", categoryid);
            if (adv == null) {
                adv = new Adv();
                long categoryidLong  =Long.parseLong(categoryid);
                adv.setCategoryid(categoryidLong);
            }
        }
        setAttr("adv", adv);
        setAttr("categoryid", categoryid);
        render("adv_edit.html");
    }


    public void editByGender() {
        Integer categoryid = getParaToInt("categoryid");
        Integer genderInt = getParaToInt("genderInt");
        if (categoryid != null) {
            Adv adv = Adv.dao.findFirst("select * from adv where categoryid =? and gender= ?", categoryid, genderInt);
            if (adv == null) {
                adv = new Adv();
                adv.setGender(genderInt);
                adv.setCategoryid((long)categoryid);
            }
            setAttr("categoryid", categoryid);
            setAttr("adv", adv);
            render("adv_edit.html");
        }
    }

    @Override
    public void save() {
        UploadFile uploadFile = getFile("img");
        String filePath = "";
        if (uploadFile != null && !StringUtils.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        Adv us = getModel(Adv.class, "adv");
        if (!StringUtils.isEmpty(filePath)) {
            us.setImg(filePath);
        }
        if (us != null && us.getId() != null) {
            us.update();
        } else {
//            us.setImg(filePath);
            us.save();
        }
        CacheKit.removeAll("news");
        CacheKit.removeAll("imageAndVideo");
        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from adv where id in (" + ids + ")");
        list();
        CacheKit.removeAll("news");
        CacheKit.removeAll("imageAndVideo");
    }

    public void pdlist() {
        String sql = "from adv where type = 2 order by id desc";
        Page<Adv> data = Adv.dao.paginate(getPageIndex(), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        render("pd_list.html");
    }

    public void pdedit() {
        Integer id = getParaToInt("id");
        Adv adv = Adv.dao.findById(id);
        setAttr("pd", adv);
        render("pd_edit.html");
    }

    public void pdsave() {
        Adv us = getModel(Adv.class, "pd");
        if (us != null && us.getId() != null) {
            us.update();
        } else {
            us.save();
        }
        CacheKit.removeAll("news");
        CacheKit.removeAll("imageAndVideo");
        pdlist();
    }

    public void pddel() {
        String ids = getPara("ids");
        Db.update("delete from adv where id in (" + ids + ")");
        CacheKit.removeAll("news");
        CacheKit.removeAll("imageAndVideo");
        pdlist();
    }
}
